<?php

class WeiboController extends Controller
{
	public function actionIndex()
	{
		$this->render('index');
	}
	public function actionBindAccount($pid  = 'P100'){ 
		$this->redirect(WeiboApiFactory::authorizeURL($pid,'/jwidget/weibo/binded'));
	}
	public function actionBind(){
		
    /**
     * 获取该pid的认证信息
     */
		$bindInfos = UserProvidor::findAllByUidPid ( Yii::app ()->user->uid,$_POST['pid'] );
    	$up=json_encode($bindInfos);
    	
//    	if($up==""){
//    		echo "请绑定微博账号";
//    	}
    	echo $js =" <script>
      	parent.weiboUP($up);
    	</script>";

	}
	
	public function actionBinded(){
		   
		$result = array();
	    if (isset($_GET['pid']) && isset($_GET['oauth_verifier'])) {
	    	
	    	$wbService = Yii::app()->wbService;
            
            
            $up = $wbService->bind($_GET['pid'],$_GET['oauth_verifier']);
            //print_r($up);
            //store
            if($up !== false){
            	$result['upid'] = $up->_id.'';
              $result['pid']=$up->pid.'';
            	$result['status'] = true;
            	
            }else{
            	$result['status'] = false;
            	
            }
		}else{
            $result['status'] = false;
		}
		
		$jsResult =  json_encode($result);
 		echo $js =" <script>
			parent.weiboBind($jsResult);
		</script>";
 		
		Yii::app()->end();
		
	}
	 
	/**
	 * weibo 显示
	 * 可分页 或只显示最新. 
	 */
	public function actionShow($page=1,$count=10){
//		echo $page;
		if(isset($_REQUEST['page'])){
			$page=$_REQUEST['page'];
//			print_r($page."; ".$_REQUEST['pid']."; ".$_REQUEST['aKey']."; ".$_REQUEST['sKey']."; ".$_REQUEST['puid']);
		}
		$client=WeiboApiFactory::client($_REQUEST['pid'],$_REQUEST['aKey'], $_REQUEST['sKey']);

		$this->renderPartial('show',(array('wbuser'=>$client->show_user($_POST['puid']),
										   'wbshow'=>$client->user_timeline($page,$count,$_REQUEST['puid']),
                                           'wbItem'=>'新浪微博','pid'=>$_REQUEST['pid'],'aKey'=>$_REQUEST['aKey'], 
                                           'sKey'=>$_REQUEST['sKey'],'page'=>$page)));
	}
}